package com.example.myapplication.view;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import com.example.myapplication.R;
import com.example.myapplication.TimeWorkUtils;
import com.example.myapplication.TodoEntity;
import com.example.myapplication.widgets.calendar.work.TimeWork;
import com.example.myapplication.databinding.FragmentSecondBinding;
import com.example.myapplication.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;

@FragmentName("周试图二（左右滑动）")
public class WeekTwoFragment extends Fragment {

    private FragmentSecondBinding binding;

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        binding = FragmentSecondBinding.inflate(inflater, container, false);
        return binding.getRoot();

    }

    String[] firstLastDate;
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

//        NavHostFragment.findNavController(WeekTwoFragment.this).popBackStack()
        binding.buttonSecond.setOnClickListener(view1 -> NavHostFragment.findNavController(WeekTwoFragment.this)
                .popBackStack());

        binding.timeTable.setOnClickWorkItemListener((date, time, nextTime, workList, timeWork) ->
                Toast.makeText(getContext(), date + " " + time + (timeWork == null ? "" : " " + timeWork.getTitle()) +
                        (workList != null && !workList.isEmpty() ? " 共" + workList.size() + "条" : ""), Toast.LENGTH_SHORT).show());

        binding.add.setOnClickListener(v -> {
            TodoEntity todoEntity = new TodoEntity();
            todoEntity.setId("新增");
            todoEntity.setName("新增");
            todoEntity.setEndTimeRangeStart("2023/05/22 9:52:52");
            todoEntity.setEndTimeRangeEnd("2023/05/22 12:52:52");
            List<TimeWork> timeWorkList = TimeWorkUtils.convertWeekTableData(getContext(), todoEntity, firstLastDate[0], firstLastDate[1]);
            binding.timeTable.addWorkList(timeWorkList);
        });
        binding.delete.setOnClickListener(v -> {
            binding.timeTable.removeWork("新增");
        });

        binding.timeTable.setShowTip(true);
        firstLastDate = binding.timeTable.setDate(2023, 5, 26);
        List<TimeWork> workList = new ArrayList<>();

        TodoEntity todoEntity = new TodoEntity();
        todoEntity.setName("呵呵爱哭的呀可垃圾啊看立刻离开家离开家");
        todoEntity.setEndTime(TimeUtils.string2Long("2023/05/26 15:52:52"));
        workList.addAll(TimeWorkUtils.convertWeekTableData(getContext(), todoEntity, firstLastDate[0], firstLastDate[1]));
        todoEntity = new TodoEntity();
        todoEntity.setName("abcdefghijklmnopqrstuvwxyz");
        todoEntity.setEndTime(TimeUtils.string2Long("2023/05/23 05:52:52"));
        workList.addAll(TimeWorkUtils.convertWeekTableData(getContext(), todoEntity, firstLastDate[0], firstLastDate[1]));
        todoEntity = new TodoEntity();
        todoEntity.setName("就剋都就看过");
        todoEntity.setEndTime(TimeUtils.string2Long("2023/05/30 23:52:52"));
        workList.addAll(TimeWorkUtils.convertWeekTableData(getContext(), todoEntity, firstLastDate[0], firstLastDate[1]));
        todoEntity = new TodoEntity();
        todoEntity.setName("时间段0501-0523");
        todoEntity.setEndTimeRangeStart("2023/05/01 05:52:36");
        todoEntity.setEndTimeRangeEnd("2023/05/23 18:52:36");
        workList.addAll(TimeWorkUtils.convertWeekTableData(getContext(), todoEntity, firstLastDate[0], firstLastDate[1]));
        todoEntity = new TodoEntity();
        todoEntity.setName("时间段0527-0531");
        todoEntity.setEndTimeRangeStart("2023/05/27 05:52:36");
        todoEntity.setEndTimeRangeEnd("2023/05/31 18:52:36");
        workList.addAll(TimeWorkUtils.convertWeekTableData(getContext(), todoEntity, firstLastDate[0], firstLastDate[1]));
        todoEntity = new TodoEntity();
        todoEntity.setName("时间段0524-0526");
        todoEntity.setEndTimeRangeStart("2023/05/24 12:52:36");
        todoEntity.setEndTimeRangeEnd("2023/05/26 14:52:36");
        workList.addAll(TimeWorkUtils.convertWeekTableData(getContext(), todoEntity, firstLastDate[0], firstLastDate[1]));
        todoEntity = new TodoEntity();
        todoEntity.setName("时间段0501-0531");
        todoEntity.setEndTimeRangeStart("2023/05/01 12:52:36");
        todoEntity.setEndTimeRangeEnd("2023/05/31 14:52:36");
        workList.addAll(TimeWorkUtils.convertWeekTableData(getContext(), todoEntity, firstLastDate[0], firstLastDate[1]));
        todoEntity = new TodoEntity();
        todoEntity.setName("时间段0527-0527");
        todoEntity.setEndTimeRangeStart("2023/05/27 10:52:36");
        todoEntity.setEndTimeRangeEnd("2023/05/27 20:52:36");
        workList.addAll(TimeWorkUtils.convertWeekTableData(getContext(), todoEntity, firstLastDate[0], firstLastDate[1]));
        binding.timeTable.setWorkList(workList);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

}